using System;
using System.Collections.Generic;
using System.Text;
using Xeml.Document;
using Mpi.Common.Collections;

namespace Xeml.Sampling
{
    public class EventColumn : Column
    {
        private TaggedAnnotation _annotation;

        private Guid _id = Guid.NewGuid();
        private EventColumnLevel _level;

        public EventColumnLevel Level
        {
            get { return _level; }
            set { _level = value; BuildNames(); }
        }


        public TaggedAnnotation Annotation
        {
            get { return _annotation; }
            set { _annotation = value; BuildNames(); }
        }


        private Event _event;

        public Event Event
        {
            get { return _event; }
            set { _event = value; BuildNames(); }
        }


        private void BuildNames()
        {
            switch (_level)
            {
                case EventColumnLevel.Time:
                    {
                        if (_event != null)
                        {
                            StringBuilder sb = new StringBuilder();
                            if (string.IsNullOrEmpty(_event.Label))
                            {
                                //base.DisplayName  = "Event@" + _event.Time.ToString();
                                base.NodeDisplayName = "Event@" + _event.TimePoint.ToString();
                            }
                            else
                            {
                                base.NodeDisplayName = _event.Label + "@" + _event.TimePoint.ToString();
                                //base.DisplayName = _event.Label + "@" + _event.Time.ToString();
                                
                            }

                            sb.AppendFormat("Event: {0}\r\n", _event.Label);
                            sb.AppendFormat("Time: {0}\r\n", _event.TimePoint);
                            base.DisplayName = sb.ToString();
                        }
                        break;
                    }
                case EventColumnLevel.Tag:
                    {
                        if (_event != null && _annotation != null)
                        {
                            base.NodeDisplayName = _annotation.Tag;
                            StringBuilder sb = new StringBuilder();

                            sb.AppendFormat("Event: {0}\r\n", _event.Label);
                            sb.AppendFormat("Time: {0}\r\n", _event.TimePoint);
                            sb.AppendFormat("Tag: {0}\r\n", _annotation.Tag);
                            base.DisplayName = sb.ToString();


                        }
                        break;
                    }
                default:
                    break;
            }
        }


        public override object ExtractRowValue(object extract)
        {
            TreeNode<StoryBase> sb = extract as TreeNode<StoryBase>;
            if (sb != null)
            {
                List<Event> historyEvents = FindPastEvents(sb, _event.TimePoint);
                if (historyEvents.Contains(_event))
                {
                    if (_level == EventColumnLevel.Tag)
                    {
                        if (_event.Annotations.Contains(_annotation))
                        {
                            return _annotation.Annotation;
                        }
                        else
                            return "-";
                    }
                    if (_level == EventColumnLevel.Time)
                    {
                        return _event.Label;
                    }
                    return "-";
                }
                else
                    return "-";
            }
            else
                return "-";
        }

        private List<Event> FindPastEvents(TreeNode<StoryBase> sb, TimeSpan timeSpan)
        {
            List<Event> ret = new List<Event>();
            TreeNode<StoryBase> x = sb;
            do
            {
                foreach (Event ev in x.Content.Events)
                {
                    if (ev.TimePoint <= timeSpan)
                        ret.Add(ev);
                }

                x = x.Parent;
            } while (!x.IsVirtualRoot) ;

            return ret;
        }

        public EventColumn()
        {
            base.name = _id.ToString();
            
        }
    }



    public enum EventColumnLevel
    {

        Time,
        Tag
    }
}
